home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / files / example3.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  7KB  |  196 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Files                       Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-15                                       */
  11. /* Version: 1.0                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program simply writes two strings to a file, moves the */
  21. /* file cursor back some characters and then collects some     */
  22. /* characters in the middle of the file. This example does     */
  23. /* exactly what is explained in picture "ReadWrite.pic".       */
  24.  
  25.  
  26.  
  27. /* Include the dos library definitions: */
  28. #include <dos/dos.h>
  29.  
  30. /* Now we include the necessary function prototype files:         */
  31. #include <clib/dos_protos.h>       /* General dos functions...    */
  32. #include <stdio.h>                 /* Std functions [printf()...] */
  33. #include <stdlib.h>                /* Std functions [exit()...]   */
  34. #include <string.h>                /* Std functions [strlen()...] */
  35.  
  36.  
  37.  
  38. /* The size of our buffer: (Remember to never */
  39. /* read more data than can fit your buffer!)  */
  40. #define MAX_LENGTH  50
  41.  
  42. /* Number of characters that we will read: */
  43. #define READ_LENGTH  7
  44.  
  45.  
  46.  
  47. /* Set name and version number: */
  48. UBYTE *version = "$VER: AmigaDOS/InputOutput/Example3 1.0";
  49.  
  50.  
  51.  
  52. /* Declared our own function(s): */
  53.  
  54. /* Our main function: */
  55. int main( int argc, char *argv[] );
  56.  
  57.  
  58.  
  59. /* Main function: */
  60.  
  61. int main( int argc, char *argv[] )
  62. {
  63.   /* A "BCPL" pointer to our file: */
  64.   BPTR my_file;
  65.  
  66.   /* The strings we want to save: */
  67.   UBYTE *string1 = "HELLO";
  68.   UBYTE *string2 = " WORLD";
  69.  
  70.   /* Some memory where the data we read can be saved: */
  71.   UBYTE my_buffer[ MAX_LENGTH ];
  72.  
  73.   /* Store here the number of characters (bytes) actually read/written: */
  74.   long actual;
  75.  
  76.   /* Old file cursor position: */
  77.   int old_pos;
  78.  
  79.  
  80.  
  81.   /* 1. Try to open file "RAM:Introduction.doc" as a new file: */
  82.   /* (If the file does not exist, it will be created. If it,   */
  83.   /* on the the other hand, exist, it will be overwritten.)    */
  84.   my_file = Open( "RAM:Introduction.doc", MODE_NEWFILE );
  85.   
  86.   /* Have we opened the file successfully? */
  87.   if( !my_file )
  88.   {
  89.     /* Inform the user: */
  90.     printf( "Error! Could not open the file!\n" );
  91.  
  92.     /* Exit with an error code: */
  93.     exit( 20 );
  94.   }
  95.  
  96.   /* The file has now been opened: */
  97.   printf( "1. File open!\n" );
  98.  
  99.  
  100.  
  101.   /* 2. We have now opened a file and the file cursor is pointing    */
  102.   /* to the first character (byte) in our new file. We can now write */
  103.   /* the first string to the file:                                   */
  104.   actual = Write( my_file, string1, strlen( string1 ) );
  105.  
  106.   /* Were all characters successfully saved? */ 
  107.   if( actual != strlen( string1 ) )
  108.   {
  109.     /* NO! Problems while writing! */
  110.     printf( "Writing error while saving the first string!\n" );
  111.   }
  112.   else
  113.     printf( "2. String 1 written!\n" );
  114.  
  115.  
  116.  
  117.   /* 3. Add the second string to the file: (Since we have not */
  118.   /* moved the file cursor since we wrote the first string    */
  119.   /* this string will be added directly after the first one.) */
  120.   actual = Write( my_file, string2, strlen( string2 ) );
  121.  
  122.   /* Were all characters successfully saved? */ 
  123.   if( actual != strlen( string2 ) )
  124.   {
  125.     /* NO! Problems while writing! */
  126.     printf( "Writing error while saving the second string!\n" );
  127.   }
  128.   else
  129.     printf( "3. String 2 written!\n" );
  130.  
  131.  
  132.  
  133.   /* 4. Move the file cursor three characters from the   */
  134.   /* beginning of the file: (We could equally well move  */
  135.   /* the file cursor eight characters backwards from the */
  136.   /* current position or the end of the file.)           */
  137.   old_pos = Seek( my_file, 3, OFFSET_BEGINNING );
  138.  
  139.   /* Tell the uer where the file cursor was and is now: */
  140.   printf( "4. File cursor moved!\n" );
  141.   printf( "     Old position: %d\n", old_pos );
  142.   printf( "     New position: 3\n" );
  143.  
  144.  
  145.  
  146.   /* 5. Collect "READ_LENGTH" (7) number of characters: */
  147.   actual = Read( my_file, my_buffer, READ_LENGTH );
  148.  
  149.   /* Did we get all characters? */ 
  150.   if( actual != READ_LENGTH )
  151.   {
  152.     /* Problems! Could not read all data as expected. */
  153.     /* We have either reached an unexpected EOF or    */
  154.     /* there was an error while we tried to read:     */
  155.     if( actual == -1 )
  156.       printf( "Error while reading!\n" );
  157.     else
  158.       printf( "Unexpected EOF!\n" );
  159.   }
  160.   else
  161.   {
  162.     /* Note that we only collected some characters in the middle */
  163.     /* of the file. There will therefore not be any NULL sign at */
  164.     /* the end of the collected string, so we have to put one    */
  165.     /* one there ourself. (If we do not put a NULL sign at the   */
  166.     /* end of the string and then later tries to print it there  */
  167.     /* will probably come a lot of junk after the collected      */
  168.     /* characters. The prinf() function will continue to print   */
  169.     /* haracters until a NULL sign is reached.)                  */
  170.     my_buffer[ READ_LENGTH ] = NULL;
  171.  
  172.     /* Some extra information: A "collection of characters" is */
  173.     /* like a string but with no NULL sign at the end, while a */
  174.     /* "string" is a collection of characters with a NULL sign */
  175.     /* at the end. Whenever you are using string functions in  */
  176.     /* C you must make sure that you realy have a string, and  */
  177.     /* not just a collection of characters. Otherwise a lot of */
  178.     /* unexpected things might happen!                         */
  179.  
  180.     /* Print the string: */
  181.     printf( "5. Read \"%s\"!\n", my_buffer );
  182.   }
  183.  
  184.  
  185.  
  186.   /* 6. Close the file: (Since we can not do very much if the */
  187.   /* function will fail to close the file we simply ignore    */
  188.   /* any returned errors.)                                    */
  189.   Close( my_file );
  190.   printf( "6. File closed!\n" );
  191.   
  192.   /* The End! */
  193.   exit( 0 );
  194. }
  195.  
  196.